<template>
  <div class="app-container">
    <el-card class="search-from">
      <el-form :inline="true"  v-model="searchfilter">
        <el-form-item label="用户名称">
          <el-input
            placeholder="请输入用户名称"
            style="width: 300px"
            ref="reffilter"
            v-model="searchfilter.filter"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" :icon="Search" @click="search()">查询</el-button>
          <el-button type="primary" :icon="Plus" @click="addUser()">添加</el-button>
        </el-form-item>
      </el-form>
    </el-card>
    <el-card  class="list">
     <div class="list-table">
      <el-table style="width: 100%" :data="dataList?.items">
        <el-table-column
          type="index"
          width="100"
          label="#"
          align="center"
        ></el-table-column>
        <template v-for="item in columns">
          <el-table-column
            :prop="item.prop"
            :label="item.label"
            :min-width="item.width"
            align="center"
          >
            <template v-slot="{ row }" v-if="item.prop == 'creationTime'">
              {{ moment(row.creationTime).format("YYYY-MM-DD HH:mm:ss") }}
            </template>
          </el-table-column>
        </template>
        <el-table-column label="操作" align="center">
          <template #default="scope">
            <el-button text type="primary" @click="editUserItem(scope.row)">修改</el-button>

            <el-button text type="primary" @click="deleteUserItem(scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      </div>
      <div class="paging">
        <el-pagination
          background
          layout="prev, pager, next"
          :total="Number(dataList?.totalCount)"
          :page-size="pageSize"
          @change="handlePageChange"
          v-model:current-page="currentPage"
        />
      </div>
    </el-card>
    <CreateUser
      v-if="addVisable"
      @closedialogevent="(eventData:boolean)=>{addVisable= eventData}"
      @refresh="pg_Init"
    />

    <EditUser
     v-if="editVisible"
     @closedialogevent="(eventData:boolean)=>{editVisible=eventData}"
     @refresh="pg_Init"

     :current-Item="currentRow"
     />
  </div>
</template>
<script setup lang="ts">
import moment from "moment";
import { Search, Plus } from "@element-plus/icons-vue";
import { getUserlist, deleteUser} from "@/api/identity/user/index";
import CreateUser from "./components/CreateUser.vue";
import EditUser from  "./components/EditUser.vue"
const currentPage = ref(1);
const pageSize = ref(10);
const dataList = ref<any[]>();
const addVisable = ref<Boolean>(false);
const editVisible = ref<boolean>(false);
const searchfilter = reactive<any>({
  filter: "",
  skipCount: (currentPage.value - 1) * pageSize.value,
  maxResultCount: pageSize.value,
});

searchfilter.filter = "";
const columns = ref([
  {
    label: "用户名",
    prop: "userName",
    width: 130,
  },
  {
    label: "姓氏",
    prop: "surname",
    width: 80,
  },
  {
    label: "名字",
    prop: "name",
    width: 80,
  },

  {
    label: "邮箱",
    prop: "email",
    width: 70,
  },
  {
    label: "手机号",
    prop: "phoneNumber",
    width: 60,
  },
  {
    label: "创建时间",
    prop: "creationTime",
    width: 90,
  },
]);

const currentRow=ref<any>({});
function pg_Init() {
  console.log("searchfilter", searchfilter);
  getUserlist(searchfilter).then((res: any) => {
    dataList.value = res;
  });
}

function search() {
  pg_Init();
}

function handlePageChange(currentPage: number, pageSize: number) {
  searchfilter.maxResultCount = pageSize;
  searchfilter.skipCount = (currentPage - 1) * pageSize;
  pg_Init();
}

const addUser = () => {
  addVisable.value = true;
};
const deleteUserItem = (row: any) => {
  ElMessageBox.confirm("确定删除吗?", "提示", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  })
    .then(() => {
      deleteUser(row.id).then((res) => {
        ElNotification({
          title: "提示",
          message: "删除用户成功",
          type: "success",
          duration: 2000,
        });
        pg_Init();
      });
      //console.log("DeleteUser", row);
    })
    .catch(() => {});
};

const editUserItem=(row:any)=>{
  currentRow.value={};
  currentRow.value=row;
  editVisible.value=true;
}
onMounted(() => {
  pg_Init();
});
</script>
<style scoped type="sass">
.header-toolbar {
  display: flex;
  justify-content: flex-end;
  align-items: center;
}
</style>
